/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.ivanbiz.service;

import com.ivanbiz.dao.InvoiceDAO;
import com.ivanbiz.dao.impl.InvoiceDAOImpl;
import com.ivanbiz.model.Invoice;
import java.util.HashMap;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.zkoss.zk.ui.Component;
import org.zkoss.zk.ui.event.Event;
import org.zkoss.zk.ui.select.SelectorComposer;
import org.zkoss.zk.ui.select.annotation.Listen;
import org.zkoss.zk.ui.select.annotation.Wire;
import org.zkoss.zul.Grid;
import org.zkoss.zul.Messagebox;
import org.zkoss.zul.Textbox;

/**
 *
 * @author IW20149X
 */
public class ReturTagihanService extends SelectorComposer<Component> {

    @Wire
    Textbox txtTagihan;
    @Wire
    Textbox noTagihan;
    @Wire
    Grid gridTagihan;

    @Listen("onClick=#cari;")
    public void cari() {
        try {
            InvoiceDAO invoiceDAO = new InvoiceDAOImpl();
            Map map = new HashMap();
            map.put("NII", txtTagihan.getText());
            map.put("status", "1");
            Invoice invoice = (Invoice) invoiceDAO.getDataByEqualMore(Invoice.class, map);
            if (invoice == null) {
                Messagebox.show("No Tagihan tidak ditemukan");
                txtTagihan.setValue("");
                noTagihan.setValue("");
            } else {
                gridTagihan.setVisible(true);
                noTagihan.setValue(invoice.getNII());
                txtTagihan.setValue("");
            }
        } catch (Exception ex) {
            Logger.getLogger(ReturTagihanService.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    @Listen("onClick=#proses;")
    public void proses() throws Exception {
        if (noTagihan.getValue().isEmpty()) {
            Messagebox.show("No Tagihan tidak ditemukan");
        } else {
            Messagebox.show("Apakah Retur Tagihan ingin di proses ?", "Confirm Dialog", Messagebox.OK | Messagebox.CANCEL, Messagebox.QUESTION, new org.zkoss.zk.ui.event.EventListener() {

                public void onEvent(Event evt) throws InterruptedException {
                    if (evt.getName().equals("onOK")) {
                        try {
                            InvoiceDAO invoiceDAO = new InvoiceDAOImpl();
                            invoiceDAO.rejectInvoice(noTagihan.getValue());
                            noTagihan.setValue("");
                            txtTagihan.setValue("");
                            gridTagihan.setVisible(false);
                        } catch (Exception ex) {
                            Logger.getLogger(ReturTagihanService.class.getName()).log(Level.SEVERE, null, ex);
                        }
                    }
                }
            });
        }
    }
}
